package com.yan.club.auth.handler;

import com.alibaba.fastjson.JSON;
import com.yan.club.auth.utils.WebUtils;
import com.yan.club.result.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.yan.club.result.ResultCode.AUTHENTICATION_FAILED;

/**
 * @author 彦
 * @since 2024/1/22 9:45
 * 自定义授权失败的处理器
 */
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {

        accessDeniedException.printStackTrace();

        Result<String> result = Result.error(AUTHENTICATION_FAILED.code, AUTHENTICATION_FAILED.msg);

        WebUtils.renderString(response, JSON.toJSONString(result));
    }
}